﻿
using HBase;
using HBase.db;
using hconnect;
using Net.http;
using System;
using System.Collections.Generic;
namespace hCommand {
    public abstract class AbsSynchro :Synchro {
        static CoreTaskParamStatus[] wcsTaskStatus = new CoreTaskParamStatus[] { CoreTaskParamStatus.满放变更 ,CoreTaskParamStatus.空取 };
        public WcsLoadUp wcsLoad;

        public IWCSServer coreTaskServer = DalFactray.getDal<IWCSServer>() ;
      

  
        public virtual MyDict<int ,TaskStatusPersistence> getTaskNo(AbsStatus status) {
            return null;
        }
        protected override void syncDb(AbsStatus cs) {
            MyDict<int ,TaskStatusPersistence> dict = getTaskNo(cs);
            if( dict != null ) {
                updataDB(dict);
            }
        }
        bool isError(int hisStatus) {
            foreach( CoreTaskParamStatus s in wcsTaskStatus ) {
                if( hisStatus == ( int ) s ) {
                    return true;
                }
            }
            return false;

        }

        public virtual void updataDB(MyDict<int ,TaskStatusPersistence> dict) {
            foreach( KeyValuePair<int ,TaskStatusPersistence> kv in dict ) {
                int taskNo = kv.Key;
                if( taskNo == 0 ) {
                    continue;
                }

                TaskStatusPersistence tsp = kv.Value;
                CoreTaskParam param = MyCache.queryUnique<CoreTaskParam>((a) => {
                    return a.taskNo == taskNo;
                } ,"CompleteParam 过多" + taskNo);
                if( param == null ) {
                    consoleOnly(InfoCode.consleOnly_task_db_null,"无有效任务{1}在站台{0}。" ,tsp.stn ,taskNo);
                    continue;

                }
                int hisStatus = param.status;
                if( isError(hisStatus) ) {
                    consoleOnly(InfoCode.consleOnly_Status_noWnok, "站台{0}的任务{1}状态异常" ,tsp.stn ,taskNo);
                    continue;
                }

                int upStatus = tsp.status;
                if( hisStatus < upStatus ) {  
                     coreTaskServer.updateTaskStatus(tsp);//顺序不能变
                    param.status = upStatus;
                } else {
                    console(InfoCode.consle_status_finsh,taskNo + "状态以完成:" + upStatus);
                }
             


            }

        }

        public List<String> error(byte[] e, Dictionary<int, String> errorsdict) {
            List<String> errors = new List<string>();
            for (int i = 0; i < e.Length; i++) {
                int b = e[i];
                for (int j = 0; j < 8; j++) {
                    if ((b & 1) == 1) {
                        int key = i * 10 + j;
                        if (errorsdict.ContainsKey(key)) {
                            String v = errorsdict[key];
                            errors.Add(v);

                        }
                    }
                    b = b >> 1;
                }
            }
            return errors;
        }



    }
}
